package com.project.dao.impl;

import com.project.bean.UserBean;
import com.project.dao.IUserDao;

import java.sql.SQLException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * DAO实现
 */
public class IUserDaoImpl extends BaseDao implements IUserDao {
    /**
     * 登陆
     * @param userName 用户名
     * @param pwd  用户密码
     * @return 返回用户对象
     */
    @Override
    public UserBean login(String userName, String pwd) {
        UserBean user = new UserBean();
//        链接
        this.setConnection();
        try {
//            执行sql语句
            ps=con.prepareStatement("select * from t_users where u_name=? and u_password=?");
//            占位符
            ps.setObject(1,userName);
            ps.setObject(2,pwd);
//            结果集
            res=ps.executeQuery();
//            循环加入
            while (res.next()){
                user.setId(res.getInt("pk_usersId"));
                user.setName(res.getString("u_name"));
                user.setPassword(res.getString("u_password"));
                user.setDate(LocalDate.parse(res.getString("u_date")));
            }
//            关闭流
            this.closeConnection();

        } catch (Exception e) {
            e.printStackTrace();
        }
    if (user.getName()!=null){
    return user;
      }
return null;

    }

    @Override
    public void add(UserBean userBean) {

        this.setConnection();
        try {
            ps=con.prepareStatement("insert into t_users(u_name,u_password,u_date) values(?,?,?)");
            ps.setObject(1,userBean.getName());
            ps.setObject(2,userBean.getPassword());
            ps.setObject(3,userBean.getDate());
            ps.executeUpdate();

//            关闭流
            this.closeConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void del(int id) {
        this.setConnection();
        try {
            ps = con.prepareStatement("delete from t_users where pk_usersId=?");
            ps.setObject(1,id);
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            this.closeConnection();
        }
    }

    @Override
    public void update(int id, String pwd) {
        this.setConnection();
        try {
            ps = con.prepareStatement("update t_users set u_password=? where pk_usersId=?");
            ps.setObject(1,pwd);
            ps.setObject(2,id);
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeConnection();
        }
    }

    @Override
    public List<UserBean> findAll() {
        List<UserBean> list = new ArrayList<>();
        this.setConnection();
        try {
            ps = con.prepareStatement("select * from t_users");
            res = ps.executeQuery();
            while (res.next()){
                list.add(this.getUser());
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            this.closeConnection();
        }
        return list;
    }

    @Override
    public UserBean findById(int id) {
        UserBean user = null;
        this.setConnection();
        try {
            ps = con.prepareStatement("select * from t_users where pk_usersId=?");
            ps.setObject(1,id);
            res = ps.executeQuery();
            while (res.next()){
                user = this.getUser();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            this.closeConnection();
        }
        return user;
    }

//    public static void main(String[] args) {
//        IUserDao dao =new IUserDaoImpl();
//        System.out.println(dao.login("张三1","234567"));
//        dao.add( new UserBean("mao1","123",LocalDate.parse("1997-05-01")));
//////        dao.del(8);
//////        dao.update(1,"123");
//////        System.out.println(dao.findAll());
////        System.out.println(dao.findById(2));
//    }
}
